﻿//修改读者信息
void ModifyReader()
{
	int ReaderID, ReaderRecord;
	int item, Num, Max;
	int FlagExist;
	int Borrow;//已经借阅的书数目

	system("cls");
	ReaderID = SearchReader();
	cout << endl;
	if (ReaderID == -1)
	{
		return;
	}
	if (ReaderID == -3)
	{
		return;
	}

	//找到该序号读者后进行修改
	ReaderRecord = ReadReaderFile("rb");

	//显示修改选项的子菜单
	cout << endl << "\t\t" << "1.编号" << "\t2.姓名" << "\t3.性别" << "\t4.最大可借阅数量" << endl;
	cout << endl << "\t请输入需要修改的相应信息(输入相应数字)：";
	cin >> item;
	if (item > 4 || item < 1)
	{
		cout << endl << "\t输入数字有误，请退出重新输入";
		return;
	}

	switch (item)
	{
	case 1:
		do {
			FlagExist = 0;
			cout << endl << "\t请输入修改后的读者编号：";
			cin >> Num;
			cout << endl;

			for (int i = 0; i < ReaderRecord; i++)
			{
				if (Num == astReader[i].Num && i != ReaderID)//数字和已有编号重复不行，更改的编号原来一样可以
				{
					FlagExist = 1;
					cout << "\t输入错误，该读者编号已经存在，请重新输入" << endl;
					break;
				}
			}
		} while (FlagExist == 1);

		astReader[ReaderID].Num = Num;
		break;
	case 2:
		cout << endl << "\t请输入修改后的读者名称：";
		cin >> astReader[ReaderID].Name;
		cout << endl;
		break;
	case 3:
		cout << endl << "\t请输入修改后的读者性别(F/M)：";
		cin >> astReader[ReaderID].Sex;
		cout << endl;
		while (astReader[ReaderID].Sex != 0)
		{
			if (strcmp(astReader[ReaderID].Sex, "F") == 0 || strcmp(astReader[ReaderID].Sex, "M") == 0)
			{
				break;
			}
			cout << "\t输入错误，只许输入'F'或'M',请重新输入";
			cin >> astReader[ReaderID].Sex;
			cout << endl;
		}
		break;
	case 4:
		Borrow = astReader[ReaderID].Max - astReader[ReaderID].Amount;
		cout << endl << "\t请输入读者最大可借阅的数量(范围为20以内)：";
		cin >> Max;
		cout << endl;

		while (Max != -1)//bug输入-1会退出
		{
			if (Max >= 0 && Max <= 20)
			{
				if (Borrow > Max)
				{
					cout << "\t请先还书!" << endl;
					return;
				}
				else
				{
					astReader[ReaderID].Max = Max;
					astReader[ReaderID].Amount = Max - Borrow;
					for (int i = Borrow; i < Max; i++)
					{
						astReader[ReaderID].BookId[i] = 0;
					}
					break;
				}
			}
			cout << "\t错误,读者最大可借阅的书的范围为20以内,请重新输入：";
			cin >> Max;
			cout << endl;
		}
		break;
	}
	//保存到文件中
	SaveReaderFile(ReaderID);
	cout << "\t读者信息修改成功";
}
